package com.EJ.ejcompanyweb;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.EJ.ejcompanyweb.HttpURLC.JsoupAsyncSearchData;
import com.EJ.ejcompanyweb.adapter.MyAdapter;
import com.EJ.ejcompanyweb.entity.DateEntity;

import java.util.ArrayList;


public class SearchActivity extends AppCompatActivity {
   private EditText searchText;
   private final String search_url="http://www.chinaags.net/index.php?route=product/search&search=";
   private RecyclerView shopList;
   private MyAdapter adapter;
    private ArrayList<DateEntity> DateEntityS=new ArrayList<DateEntity>() ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏这标题兰
        getSupportActionBar().hide();
        setContentView(R.layout.activity_search);
        initView();
    }

    private void initView() {
        searchText=findViewById(R.id.search_editText);
        Button searchButton=findViewById(R.id.search_btn);
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startSearch(searchText.getText().toString());
            }
        });

        shopList=findViewById(R.id.shop_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        shopList.setLayoutManager(layoutManager);
        adapter=new MyAdapter(DateEntityS);
        shopList.setAdapter(adapter);
        //shopList.invalidate();

        findViewById(R.id.search_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void startSearch(String searchMessge){
        if (searchMessge==null){
            return;
        }
        //搜索请求的url
       //search_url.concat(searchMessge);
        if (!DateEntityS.isEmpty()){
            DateEntityS.clear();
            adapter.notifyDataSetChanged();
        }
        new JsoupAsyncSearchData(this,shopList,adapter,DateEntityS).execute(search_url.concat(searchMessge));
    }

    public static void activityStart( Activity context)
    {
        Intent intent = new Intent(context, SearchActivity.class);
        context.startActivity(intent);
    }
}
